package mr.ali.video.web.controller;


import lombok.extern.slf4j.Slf4j;
import mr.ali.video.web.Exception.R;
import mr.ali.video.web.model.CollectionUrl;
import mr.ali.video.web.model.Episode;
import mr.ali.video.web.service.ICollectionUrlService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-03
 */
@RestController
@RequestMapping("/web/collectionUrl")
@Slf4j
public class CollectionUrlController {
    @Resource
    private ICollectionUrlService collectionUrlService;

    @GetMapping("/selectById")
    public List<CollectionUrl> selectById(Integer id) {
        return collectionUrlService.selectByCollectionId(id);
    }
    // 2. Controller 方法
    @PostMapping("/insert")
    public R<ResponseEntity<String>> insert(@RequestBody List<Episode> episodes) {
        log.info("接收到的剧集列表: {}", episodes.get(0).getEpisode());
        for (int i =0 ; i <episodes.size(); i++) {
            CollectionUrl collectionUrl = new CollectionUrl();
            collectionUrl.setVideoId(episodes.get(i).getVideoId());
            collectionUrl.setVideoJi(episodes.get(i).getEpisode());
            collectionUrl.setVideoUrl(episodes.get(i).getVideoUrl());
            collectionUrl.setStatus(episodes.get(i).getStatus());
            collectionUrl.setGmtCreate(System.currentTimeMillis());
            log.info("插入数据: {}", collectionUrl);
            if (episodes.get(i).getEpisode() != null) {
                collectionUrlService.insert(collectionUrl);
            }
        }

        // 在这里处理插入逻辑（保存到数据库等）

        return R.ok(ResponseEntity.ok("成功插入 " + episodes.size() + " 条数据"));
    }
}
